import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

/// 按钮背景 - 红色
const buttonBackgroundRed = LinearGradient(
  colors: [Color(0xFFF20300), Color(0xFFFF4D17)],
);

/// 按钮背景 - 黄色
const buttonBackgroundYellow = LinearGradient(
  colors: [Color(0xFFFFA700), Color(0xFFFFBB00)],
);

/// 按钮背景 - 蓝色
const buttonBackgroundBlue = LinearGradient(
  colors: [Color(0xFF2F9DFE), Color(0xFF56CDFE)],
);

/// 按钮默认的圆角
const defaultButtonRadius = BorderRadius.all(Radius.circular(22));

/// 按钮默认的间距
const defaultButtonMargin = EdgeInsets.symmetric(horizontal: 25);

/// 通用的按钮
class MyButtonWidget extends StatelessWidget {
  const MyButtonWidget({
    super.key,
    required this.text,
    this.textColor = Colors.white,
    this.icon,
    this.width = double.infinity,
    this.height = 44,
    this.margin = defaultButtonMargin,
    this.backgroundGradient = buttonBackgroundRed,
    this.borderRadius = defaultButtonRadius,
    this.onTap,
  });

  final String text;
  final Color textColor;
  final String? icon;
  final double width;
  final double height;
  final EdgeInsetsGeometry? margin;
  final Gradient backgroundGradient;
  final BorderRadiusGeometry borderRadius;
  final GestureTapCallback? onTap;

  @override
  Widget build(BuildContext context) {
    Widget child = Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        if (!TextUtil.isEmpty(icon))
          Padding(
            padding: EdgeInsets.only(right: 5.w),
            child: Image.asset(icon ?? '', width: 18.w, height: 18.w),
          ),
        Text(
          text,
          style: TextStyle(fontSize: 16.sp, color: textColor),
        ),
      ],
    );
    child = Container(
      width: width.w,
      height: height.w,
      margin: margin,
      alignment: Alignment.center,
      decoration: BoxDecoration(
        gradient: backgroundGradient,
        borderRadius: borderRadius,
      ),
      child: child,
    );
    return GestureDetector(onTap: onTap, child: child);
  }
}
